package com.zidian.study.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mingming.com.library.FlowTagLayout;
import com.mingming.com.library.OnTagSelectListener;
import com.zidian.student.trucks.R;
import com.zidian.study.bean.TagsBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by GongCheng on 2016/11/30.
 */

public class EvaTagAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<TagsBean.DataBean.PushLabelBean> data ;
    private List<TagsBean.DataBean.SubmitLabelBean> submitLabels;
    private Map<Integer, String> map ;
    private StringBuffer  sb;

    public EvaTagAdapter() {
        map = new HashMap<>();
        sb = new StringBuffer();
        submitLabels = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_eva_tag, parent, false);

        return new TagViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof  TagViewHolder) {
            ((TagViewHolder) holder).evaTitleTv.setText(data.get(position).getThreeIndexQuestion());

            ((TagViewHolder) holder).evaTags.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_SINGLE);
            ((TagViewHolder) holder).evaTags.setAdapter(((TagViewHolder) holder).textAdapter);
            //翻转
            List<String> list  = new ArrayList<>();
            for (int i = data.get(position).getLabel().size() - 1; i >= 0; i--) {
                list.add(data.get(position).getLabel().get(i));
            }
            ((TagViewHolder) holder).textAdapter.clearAndAddAll(list);
            //判断是否需要预选
            if (submitLabels.size() != 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).equals(submitLabels.get(position).getEvaluateLabel())) {
                        ((TagViewHolder) holder).textAdapter.setSelectedItem(i);
                        ((TagViewHolder) holder).textAdapter.notifyDataSetChanged();
                    }
                }
            }

            ((TagViewHolder) holder).evaTags.setOnTagSelectListener(new OnTagSelectListener() {
                @Override
                public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList) {
                    if (selectedList != null && selectedList.size() > 0) {
                        StringBuilder sb = new StringBuilder();
                        for (int i : selectedList) {

                            sb.append("教评!")
                                    .append(data.get(holder.getAdapterPosition()).getThreeIndexName())
                                    .append("!")
                                    .append(parent.getAdapter().getItem(i));
                        }
                        map.put(holder.getAdapterPosition() ,sb.toString());
                    }
                }
            });
        }
    }

    /**
     * 得到评价信息
     * @return
     */
    public Map<Integer, String> getSelected() {
        return map;
    }
    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void setData(List<TagsBean.DataBean.PushLabelBean> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * 设置已选中标签
     * @param submitLabels
     */
    public void setSubmitLabels(List<TagsBean.DataBean.SubmitLabelBean> submitLabels) {
        this.submitLabels = submitLabels;
    }

    static class TagViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.eva_title_tv)
        TextView evaTitleTv;
        @Bind(R.id.eva_tags)
        FlowTagLayout evaTags;

        TagTextAdapter textAdapter;

        public TagViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            textAdapter = new TagTextAdapter(evaTags.getContext());
        }
    }
}
